package com.stlm2.controller.user;

import com.stlm2.core.base.BaseController;
import com.stlm2.dems.user.service.UserRolePrivilegeService;
import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by zlq on 2017/8/3.
 */
@Controller
@RequestMapping("/userRolePrivilegeC")
public class UserRolePrivilegeController extends BaseController {
    @Resource
    UserRolePrivilegeService userRolePrivilegeService;


    /*zlq
*资源树页面
* */
    @ResponseBody
    @RequestMapping(value="/tree",produces="application/json;charset=UTF-8")
    public ModelAndView listView(ModelAndView modelAndView){
        modelAndView.setViewName("/tree/tree");
        return modelAndView;
    }
    /*
*查找角色拥有的资源树
* zlq
*@param  token
* @param  roleId ：角色ID
*
*/
    @ResponseBody
    @RequestMapping(value="/getByRolePrivilege.json",produces="application/json;charset=UTF-8")
    public String  getByRolePrivilege(ModelMap map,String token,String roleId){
        String data=null;
        try {
            data=userRolePrivilegeService.getByRolePrivilege(map,token,roleId);
        }catch (Exception e){
            e.printStackTrace();
            return CodeContent.returnFailure("搜索失败");
        }
        return  data;
    }

    /*
*给角色添加权限
* *zlq
* @param  token
* @param  roleId ：角色ID
* @param  permissionIds:多个资源权限ID拼接的字符串
 */
    @ResponseBody
    @RequestMapping(value="/roleAddPermission.json",produces="application/json;charset=UTF-8")
    public String  roleAddPermission(ModelMap map,String token,String roleId,String permissionIds){
        String data=null;
        try {
            data=userRolePrivilegeService.roleAddPermission(map,token,roleId,permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            return CodeContent.returnFailure("添加角色权限失败");
        }
        return  data;
    }


    	/*
*给用户添加权限
* *zlq
* @param  token
* @param  userId ：用户ID
* @param  permissionIds:多个权限ID拼接的字符串(以“ ，”逗号隔开)
*
 */
        @ResponseBody
        @RequestMapping(value="/userAddPermission.json",produces="application/json;charset=UTF-8")
        public String  userAddPermission(ModelMap map,String token,String userId,String permissionIds){
            String data=null;
            try {
                data=userRolePrivilegeService.userAddPermission(map,token,userId,permissionIds);
            }catch (Exception e){
                e.printStackTrace();
                return CodeContent.returnFailure("添加用户权限失败");
            }
            return  data;
        }


    /*
*查找用户资源树
* zlq
*@param  token
* @param  userId ：用户ID
*
*/
    @ResponseBody
    @RequestMapping(value="/getByUserPrivilege.json",produces="application/json;charset=UTF-8")
    public String  getByUserPrivilege(ModelMap map,String token,String userId){
        String data=null;
        try {
            data=userRolePrivilegeService.getByUserPrivilege(map,token,userId);
        }catch (Exception e){
            e.printStackTrace();
            return CodeContent.returnFailure("查找用户资源权限失败");
        }
        return  data;
    }
}
